<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Question extends CI_Controller
{
    protected $menu = array(
	'menu_1' => 'active',
	'menu_2' => '',
	'menu_3' => '',
	'menu_4' => '',
    );
    
    function __construct()
    {
	parent::__construct();
	
	if( ! $this->_checkAuth()){
	    redirect('admin/login');
	    exit;
	}
    }
    
    function index()
    {
	$this->mytpl->set_template('admin-bootstrap/menu.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($this->menu);
	$data['menu'] = $this->mytpl->html();
	
	$this->initmodel->set_table_name('question');
	$this->initmodel->set_where(array('status'=>'1'));
	$questions = $this->initmodel->load_records();
	$var['questions'] = array();
	$i = 0;
	foreach($questions as $val){
	    $var['questions'][$i]['id'] = $val['id'];
	    $var['questions'][$i]['remark'] = $val['remark'];
	    $var['questions'][$i]['question_img'] = $val['question_img'];
	    $var['questions'][$i]['correct_id'] = $val['correct_id'];
	    
	    $answer = json_decode($val['answers']);
	    foreach($answer as $key=>$val){
		$var['questions'][$i]["answer_{$key}"]['title'] = $val->title;
		$var['questions'][$i]["answer_{$key}"]['img'] = $val->img;
	    }
	    
	    $i++;
	}
	
	$this->mytpl->set_template('admin-bootstrap/question.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($var);
	$data['content'] = $this->mytpl->html();
	
	$this->mytpl->set_template('admin-bootstrap/main.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($data);
	$this->mytpl->display();
    }
    
    function add_form()
    {
	$this->mytpl->set_template('admin-bootstrap/menu.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($this->menu);
	$data['menu'] = $this->mytpl->html();
	
	$this->mytpl->set_template('admin-bootstrap/question-add-form.html');
	$this->mytpl->clear_cache();
	$data['content'] = $this->mytpl->html();
	
	$this->mytpl->set_template('admin-bootstrap/main.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($data);
	$this->mytpl->display();
    }
    
    function add()
    {
	if ($this->input->post('post_check')) {
	    $input['remark'] = $this->input->post('remark');
	    $input['correct_id'] = $this->input->post('corect');
	    
	    $this->initmodel->set_table_name('question');
	    $question_id = $this->initmodel->insert_record($input);
	    
	    if($question_id){
		$this->load->library('ciupload',$_FILES['question_img']);
		if ($this->ciupload->uploaded ) {
		    $this->ciupload->image_resize = true;
		    $this->ciupload->image_ratio_fill = true;
		    $this->ciupload->image_x = 530;
		    $this->ciupload->image_y = 167;
		    $this->ciupload->process(FCPATH."assets/images/questions/{$question_id}/");
		    
		    if($this->ciupload->processed){
			$this->ciupload->clean();
			$input['question_img'] = $this->ciupload->file_dst_name;
		    }
		}
		
		$answer['1']['title'] = $this->input->post('answer1_title');
		$this->ciupload->upload($_FILES['answer1_img']);
		if ($this->ciupload->uploaded ) {
		    $this->ciupload->image_resize = true;
		    $this->ciupload->image_ratio_fill = true;
		    $this->ciupload->image_x = 77;
		    $this->ciupload->image_y = 77;
		    $this->ciupload->process(FCPATH."assets/images/questions/{$question_id}/");
		    
		    if($this->ciupload->processed){
			$this->ciupload->clean();
			$answer['1']['img'] = $this->ciupload->file_dst_name;
		    }
		}
		
		$answer['2']['title'] = $this->input->post('answer2_title');
		$this->ciupload->upload($_FILES['answer2_img']);
		if ($this->ciupload->uploaded ) {
		    $this->ciupload->image_resize = true;
		    $this->ciupload->image_ratio_fill = true;
		    $this->ciupload->image_x = 77;
		    $this->ciupload->image_y = 77;
		    $this->ciupload->process(FCPATH."assets/images/questions/{$question_id}/");
		    
		    if($this->ciupload->processed){
			$this->ciupload->clean();
			$answer['2']['img'] = $this->ciupload->file_dst_name;
		    }
		}
		
		$answer['3']['title'] = $this->input->post('answer3_title');
		$this->ciupload->upload($_FILES['answer3_img']);
		if ($this->ciupload->uploaded ) {
		    $this->ciupload->image_resize = true;
		    $this->ciupload->image_ratio_fill = true;
		    $this->ciupload->image_x = 77;
		    $this->ciupload->image_y = 77;
		    $this->ciupload->process(FCPATH."assets/images/questions/{$question_id}/");
		    
		    if($this->ciupload->processed){
			$this->ciupload->clean();
			$answer['3']['img'] = $this->ciupload->file_dst_name;
		    }
		}
		
		$answer['4']['title'] = $this->input->post('answer4_title');
		$this->ciupload->upload($_FILES['answer4_img']);
		if ($this->ciupload->uploaded ) {
		    $this->ciupload->image_resize = true;
		    $this->ciupload->image_ratio_fill = true;
		    $this->ciupload->image_x = 77;
		    $this->ciupload->image_y = 77;
		    $this->ciupload->process(FCPATH."assets/images/questions/{$question_id}/");
		    
		    if($this->ciupload->processed){
			$this->ciupload->clean();
			$answer['4']['img'] = $this->ciupload->file_dst_name;
		    }
		}
		
		$input['answers'] = json_encode($answer);
		$this->initmodel->set_where(array('id'=>$question_id));
		$this->initmodel->update_record($input);
	    }
	}
	
	redirect('admin/question');
    }
    
    function edit_form($question_id)
    {
	$this->mytpl->set_template('admin-bootstrap/menu.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($this->menu);
	$data['menu'] = $this->mytpl->html();
	
	$this->initmodel->set_table_name('question');
	$this->initmodel->set_where(array('id'=>$question_id));
	$question = $this->initmodel->load_records();
	$var['question_id'] = $question_id;
	$var['question_img'] = $question['0']['question_img'];
	$var['remark'] = $question['0']['remark'];
	$var['correct_id'] = $question['0']['correct_id'];
	$answers = json_decode($question['0']['answers']);
	foreach($answers as $kay=>$val){
	    $var["answer{$kay}_title"] = $val->title;
	    $var["answer{$kay}_img"] = $val->img;
	}
	
	$this->mytpl->set_template('admin-bootstrap/question-edit-form.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($var);
	$data['content'] = $this->mytpl->html();
	
	$this->mytpl->set_template('admin-bootstrap/main.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($data);
	$this->mytpl->display();
    }
    
    function edit()
    {
	if ($this->input->post('post_check')) {
	    $question_id = $this->input->post('question_id');
	    $input['remark'] = $this->input->post('remark');
	    $input['correct_id'] = $this->input->post('corect');
	    
	    $this->load->library('ciupload',$_FILES['question_img']);
	    if ($this->ciupload->uploaded ) {
		$this->ciupload->image_resize = true;
		$this->ciupload->image_ratio_fill = true;
		$this->ciupload->image_x = 530;
		$this->ciupload->image_y = 167;
		$this->ciupload->process(FCPATH."assets/images/questions/{$question_id}/");
		
		if($this->ciupload->processed){
		    $this->ciupload->clean();
		    $input['question_img'] = $this->ciupload->file_dst_name;
		}
	    }
	    
	    $answer['1']['title'] = $this->input->post('answer1_title');
	    $this->ciupload->upload($_FILES['answer1_img']);
	    if ($this->ciupload->uploaded ) {
		$this->ciupload->image_resize = true;
		$this->ciupload->image_ratio_fill = true;
		$this->ciupload->image_x = 77;
		$this->ciupload->image_y = 77;
		$this->ciupload->process(FCPATH."assets/images/questions/{$question_id}/");
		
		if($this->ciupload->processed){
		    $this->ciupload->clean();
		    $answer['1']['img'] = $this->ciupload->file_dst_name;
		}
	    }
	    
	    $answer['2']['title'] = $this->input->post('answer2_title');
	    $this->ciupload->upload($_FILES['answer2_img']);
	    if ($this->ciupload->uploaded ) {
		$this->ciupload->image_resize = true;
		$this->ciupload->image_ratio_fill = true;
		$this->ciupload->image_x = 77;
		$this->ciupload->image_y = 77;
		$this->ciupload->process(FCPATH."assets/images/questions/{$question_id}/");
		
		if($this->ciupload->processed){
		    $this->ciupload->clean();
		    $answer['2']['img'] = $this->ciupload->file_dst_name;
		}
	    }
	    
	    $answer['3']['title'] = $this->input->post('answer3_title');
	    $this->ciupload->upload($_FILES['answer3_img']);
	    if ($this->ciupload->uploaded ) {
		$this->ciupload->image_resize = true;
		$this->ciupload->image_ratio_fill = true;
		$this->ciupload->image_x = 77;
		$this->ciupload->image_y = 77;
		$this->ciupload->process(FCPATH."assets/images/questions/{$question_id}/");
		
		if($this->ciupload->processed){
		    $this->ciupload->clean();
		    $answer['3']['img'] = $this->ciupload->file_dst_name;
		}
	    }
	    
	    $answer['4']['title'] = $this->input->post('answer4_title');
	    $this->ciupload->upload($_FILES['answer4_img']);
	    if ($this->ciupload->uploaded ) {
		$this->ciupload->image_resize = true;
		$this->ciupload->image_ratio_fill = true;
		$this->ciupload->image_x = 77;
		$this->ciupload->image_y = 77;
		$this->ciupload->process(FCPATH."assets/images/questions/{$question_id}/");
		
		if($this->ciupload->processed){
		    $this->ciupload->clean();
		    $answer['4']['img'] = $this->ciupload->file_dst_name;
		}
	    }
	    
	    $input['answers'] = json_encode($answer);
	    
	    $this->initmodel->set_table_name('question');
	    $this->initmodel->set_where(array('id'=>$question_id));
	    $this->initmodel->update_record($input);
	}
	
	redirect('admin/question');
    }
    
    function delete($id)
    {
	$data['status'] = '0';
	$this->initmodel->set_table_name('question');
	$this->initmodel->set_where(array('id'=>$id));
	$this->initmodel->update_record($data);
	redirect('admin/question');
    }
    
    /**
    * private function to use in this controller
    */
    function _checkAuth()
    {
	$admin = $this->session->userdata('admin');
	if (isset($admin['user']) && $admin['user'] == 'admin') {
	    return true;
	} else {
	    return false;
	}
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */